package com.nwcl.common.result;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


public class Page<T> implements Serializable {

    private int pageNum = 1;
    private int pageSize = 10;
    private long total;
    private int pageTotal;//当前页面总数量
    private List<T> list = new ArrayList<T>();
    public Page() {
    }

    public Page(int pageNum, int pageSize, long total) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total > 0 ? total : 0;
        if (pageSize > 0) {
            this.pageTotal = Integer.parseInt(String.valueOf((total / pageSize) + (total % pageSize > 0 ? 1 : 0)));
        } else {
            this.pageTotal = 0;
        }

    }

    public Page(int pageNum, int pageSize, long total, List<T> list) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.total = total > 0 ? total : 0;
        this.list = list;
        if (pageSize > 0) {
            this.pageTotal = Integer.parseInt(String.valueOf((total / pageSize) + (total % pageSize > 0 ? 1 : 0)));
        } else {
            this.pageTotal = 0;
        }

    }

    public static Page getPageList(Page page) {
        List boList = page.getList();
        int total = (int) page.getTotal();
        int start = (page.getPageNum() - 1) * page.getPageSize();
        int end = page.getPageSize() + start;
        if (start > total) {
            return new Page<>();
        }
        end = end > total ? total : end;
        boList = boList.subList(start, end);
        page.setList(boList);
        page.setPageSize(page.getPageSize());
        return page;
    }


    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getPageTotal() {
        return pageTotal;
    }

    public void setPageTotal(int pageTotal) {
        this.pageTotal = pageTotal;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }


}

